{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Solution:\n",
    "    def simplifyPath(self, path: str) -> str:\n",
    "        ls =[]\n",
    "        curdir = ''\n",
    "        i_pre = ''\n",
    "        for i in path:\n",
    "            if( i == '/'):\n",
    "                if(i_pre != '/' and len(curdir) > 0):\n",
    "                    ls.append(curdir)\n",
    "                    curdir = ''\n",
    "            else:\n",
    "                curdir +=i\n",
    "        if(curdir):\n",
    "            ls.append(curdir)\n",
    "        que = []\n",
    "        for d in ls:\n",
    "            if(d == '..'):\n",
    "                if(que):\n",
    "                    que.pop()\n",
    "            elif(d == '.'):\n",
    "                pass\n",
    "            else: #3个以上的.表示文件名?\n",
    "                que.append(d)\n",
    "        path = ''\n",
    "        for d in que:\n",
    "            path += '/' + d\n",
    "        if(len(que) == 0):\n",
    "            path = '/'\n",
    "        return path\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/home/foo\n",
      "/\n",
      "/\n",
      "/\n",
      "/a/b/c/d/e/.../f\n",
      "/a/b/c/d/e/...\n",
      "/c\n",
      "/a/b/c\n",
      "/...\n"
     ]
    }
   ],
   "source": [
    "a = Solution()\n",
    "ls = ['/home//foo/',\n",
    "      '///',\n",
    "      '/..',\n",
    "      '/../',\n",
    "      '/a/b/c/d/e/.../f',\n",
    "      '/a/b/c/d/e/.../',\n",
    "      \"/a/../../b/../c//.//\",\n",
    "      \"/a//b////c/d//././/..\",\n",
    "      '/...'\n",
    "     ]\n",
    "for i in ls:\n",
    "    print(a.simplifyPath(i))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Example 1:\n",
    "\n",
    "Input: \"/home/\"\n",
    "Output: \"/home\"\n",
    "Explanation: Note that there is no trailing slash after the last directory name.\n",
    "Example 2:\n",
    "\n",
    "Input: \"/../\"\n",
    "Output: \"/\"\n",
    "Explanation: Going one level up from the root directory is a no-op, as the root level is the highest level you can go.\n",
    "Example 3:\n",
    "\n",
    "Input: \"/home//foo/\"\n",
    "Output: \"/home/foo\"\n",
    "Explanation: In the canonical path, multiple consecutive slashes are replaced by a single one.\n",
    "Example 4:\n",
    "\n",
    "Input: \"/a/./b/../../c/\"\n",
    "Output: \"/c\"\n",
    "Example 5:\n",
    "\n",
    "Input: \"/a/../../b/../c//.//\"\n",
    "Output: \"/c\"\n",
    "Example 6:\n",
    "\n",
    "Input: \"/a//b////c/d//././/..\"\n",
    "Output: \"/a/b/c\"\n",
    "    \n",
    "输入:\n",
    "\"/...\"\n",
    "输出\n",
    "\"/...\"\n",
    "\n",
    "来源：力扣（LeetCode）\n",
    "链接：https://leetcode-cn.com/problems/simplify-path\n",
    "著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
